#include "window_common.h"

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_SELECT,
	WIDGET_ID_HINT,
	WIDGET_ID_CANCEL_OK,
	WIDGET_ID_MAX
};

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_MAX
};

static const gkt_widget_select_attr_u sc_window_lock_auto_close_time_widget_select_attr = {
	GKT_WIDGET_SELECT_NUMBER_ATTR(WINDOW_LOCK_AUTO_CLOSE_TIME_MIN, 
		WINDOW_LOCK_AUTO_CLOSE_TIME_MAX, 1,
		WINDOW_LOCK_AUTO_CLOSE_TIME_DEFAULT, 
		WINDOW_TEXT_ID_sec),
};

GKT_WINDOW_WIDGET_DESCS_DEFINE(lock_auto_close_time) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, 
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_auto_close_time
	),
	[WIDGET_ID_SELECT] = GKT_WIDGET_SELECT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_LARGE_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT, 
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		&sc_window_lock_auto_close_time_widget_select_attr
	),
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION) \
	&& (defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC)
	[WIDGET_ID_HINT] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, (WINDOW_WIDGET_NORMAL_HEIGHT / 2) * 3, 
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		NULL),
#endif
	[WIDGET_ID_CANCEL_OK] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * 3, 
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_cancel_ok
	),
};

typedef struct _window_lock_time {
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION) \
	&& (defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC)
	int	update_setting;
#endif
	uint32_t	auto_close_time;
} window_lock_time_s;
static window_lock_time_s s_window_lock_time;

static void window_lock_auto_close_time_success(void)
{
	if(s_window_lock_time.auto_close_time != gp_window_data->lock_auto_close_time) {
		gp_window_data->lock_auto_close_time = s_window_lock_time.auto_close_time;
		window_data_save();
	}

	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_SUCCESS,
			WINDOW_TEXT_ID_auto_close_time, 
			WINDOW_TEXT_ID_set_success,
			WINDOW_VOICE_COMBINED_ID(set_success));
	gkt_window_exit();
}

#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION) \
	&& (defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC)
#define window_lock_auto_close_time_init()	\
	do { s_window_lock_time.update_setting = 0; } while (0)
#define window_lock_auto_close_time_active()	\
	do { gkt_widget_set_showable_by_id(WIDGET_ID_HINT, 0); } while (0)

static void window_lock_auto_close_time_failed(void)
{
	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_FAILED,
			WINDOW_TEXT_ID_auto_close_time, 
			WINDOW_TEXT_ID_set_fail,
			WINDOW_VOICE_COMBINED_ID(set_failed));
}

static void window_lock_auto_close_time_update(void)
{
	int retval;

	window_common_mask_all();
	gkt_widget_hide_by_id(WIDGET_ID_SELECT);
	gkt_widget_set_data_by_id(WIDGET_ID_HINT, WINDOW_TEXT_ID_update_setting);
	gkt_widget_set_showable_by_id(WIDGET_ID_HINT, 1);
	gkt_widget_show_by_id(WIDGET_ID_HINT);
	gkt_widget_hide_by_id(WIDGET_ID_CANCEL_OK);

	retval = gkt_door_lock_set_auto_close_time(0, s_window_lock_time.auto_close_time);
	if (GKT_EINPROCESS == retval) {
		s_window_lock_time.update_setting = 1;
		gkt_window_timer_set(TIMER_ID_TIMEOUT, 2000, 0, 0);
	}
	else
		window_lock_auto_close_time_failed();
}
#else
#define window_lock_auto_close_time_init()	do {} while (0)
#define window_lock_auto_close_time_active()	do {} while (0)
#define window_lock_auto_close_time_update()	\
	window_lock_auto_close_time_success()
#endif

static int window_lock_auto_close_time_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	gkt_info("lock_auto_close_time: (%u)(%u-%u)\n",
		minor_type, id, data);

	switch (major_type) {
		case GKT_EVENT_WINDOW:
			if (minor_type == GKT_WINDOW_INIT) {
				window_lock_auto_close_time_init();
				s_window_lock_time.auto_close_time = gp_window_data->lock_auto_close_time;
				if ((s_window_lock_time.auto_close_time < WINDOW_LOCK_AUTO_CLOSE_TIME_MIN)
					|| (s_window_lock_time.auto_close_time > WINDOW_LOCK_AUTO_CLOSE_TIME_MAX))
				{
					s_window_lock_time.auto_close_time = WINDOW_LOCK_AUTO_CLOSE_TIME_DEFAULT;
				}
			}
			else if (minor_type == GKT_WINDOW_ACTIVE) {
				window_common_mask_except_updn_cancel_ok();
				if (data) {
					gkt_widget_select_set_value(WIDGET_ID_SELECT, 
						s_window_lock_time.auto_close_time);
				}
				window_lock_auto_close_time_active();
			}
			break;
		case GKT_EVENT_WIDGET:
			if (minor_type == GKT_WIDGET_EVENT_CLICK) {
				s_window_lock_time.auto_close_time = gkt_widget_select_get_value(WIDGET_ID_SELECT);
				window_lock_auto_close_time_update();
				return GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION) \
	&& (defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC)
		case GKT_EVENT_SYSTEM:
			if ((GKT_WINDOW_SYSTEM_DOOR == minor_type)
				&& (GKT_DOOR_NOTIFY_LOCK_CONFIG == id)
				&& s_window_lock_time.update_setting)
			{
				s_window_lock_time.update_setting = 0;
				gkt_window_timer_clear(TIMER_ID_TIMEOUT);
				if (GKT_SUCCESS == data)
					window_lock_auto_close_time_success();
				else
					window_lock_auto_close_time_failed();
			}
			break;
		case GKT_EVENT_TIMER:
			if ((minor_type == TIMER_ID_TIMEOUT)
				&& s_window_lock_time.update_setting)
			{
				s_window_lock_time.update_setting = 0;
				window_lock_auto_close_time_failed();
			}
			break;
#endif
		default:
			break;
	}

	return GKT_EVENT_HANDLE_CONTINUE;
}
GKT_WINDOW_DESC_DEFINE(lock_auto_close_time);

